package org.example;

import org.example.loadBalancers.LoadBalanceContext;
import org.example.loadBalancers.LoadBalancer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.List;


//用于创建bean  之后可用于暴露给外面
@Configuration
@ComponentScan(basePackages = "org.example.loadBalancers")  // 自动扫描策略类
public class MyConfiguration {
    @Bean
    public CustomConfig customConfig() {
        return new CustomConfig();
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    public DiscoveryService discoveryService() {
        return new DiscoveryService();
    }

    @Bean
    public  ServiceContext serviceContext() {
        return new ServiceContext();
    }

    @Bean
    public  ListenerController listenerController() {
        return new ListenerController();
    }


    //负载均衡器 会被自动注入
    @Bean
    public LoadBalanceContext loadbalanceContext(List<LoadBalancer> loadBalancers){
        LoadBalanceContext loadBalanceContext = new LoadBalanceContext();
        for (LoadBalancer loadBalancer:loadBalancers) {
            loadBalanceContext.registerLoadBalancer(loadBalancer.getName(),loadBalancer);
        }
        return loadBalanceContext;
    }

}
